#!/usr/bin/env python3
# coding: utf-8
# File: answer_search.py
# Author: lhy<lhy_in_blcu@126.com,https://huangyong.github.io>
# Date: 18-10-5

from py2neo import Graph


class AnswerSearcher:
    def __init__(self):
        # self.g = Graph(
        #     host="127.0.0.1",
        #     http_port=7474,
        #     user="neo4j",
        #     password="123456")
        self.g = Graph("bolt://192.168.22.69:7687", username="neo4j", password="shumei123")
        # self.g = Graph("bolt://localhost:7687", username="neo4j", password="123456")
        self.num_limit = 20

    '''执行cypher查询，并返回相应结果'''
    def search_main(self, sqls):
        final_answers = []
        for sql_ in sqls:
            question_type = sql_['question_type']
            queries = sql_['sql']
            answers = []
            for query in queries:
                ress = self.g.run(query).data()
                answers += ress
            final_answer = self.answer_prettify(question_type, answers)
            if final_answer:
                final_answers.append(final_answer)
        return final_answers

    '''根据对应的qustion_type，调用相应的回复模板'''
    def answer_prettify(self, question_type, answers):
        final_answer = []
        if not answers:
            return ''
        if question_type == 'disease_symptom':
            desc = [i['n.name'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}的症状包括：{1}\n'.format(subject, ','.join(list(set(desc))[:self.num_limit]))+'\n'

        # elif question_type == 'symptom_disease':
        #     desc = [i['m.name'] for i in answers]
        #     subject = answers[0]['n.name']
        #     final_answer = '{0}可能染上的疾病大约有{1}种，如{2}等，您可以输入更多的症状来进行查询\n'.format(subject,len(desc),','.join(list(set(desc[:5]))[:self.num_limit]))+'\n'
        #
        elif question_type == 'symptom_disease':
            desc = answers[0]['skillset']
            matches = answers[0]['matches']
            subject1 = []
            for answer in answers:
                if answer['matches'] == matches:
                    subject1.append(answer['m.name'])
            # final_answer1 = '{0}等症状可能染上的疾病有：{1}'.format('、'.join(desc), '、'.join(subject1))
            if len(subject1) >3:
                final_answer1 = '{0}可能染上的疾病大约有{1}种，如{2}等，您可以输入更多的症状来进行查询\n'.format('、'.join(desc), len(subject1), '、'.join(subject1[:5]))+'\n'
            else:
                final_answer1 = '{0}染上的疾病可能是{1}。'.format('、'.join(desc), '、'.join(subject1[:5])) + '\n'
            final_answer = final_answer1.strip()

        elif question_type == 'disease_cause':
            desc = [i['m.cause'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}可能的成因有：{1}\n'.format(subject, ','.join(list(set(desc))[:self.num_limit]))+'\n'

        elif question_type == 'disease_prevent':
            desc = [i['m.prevent'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}的预防措施包括：{1}\n'.format(subject, ','.join(list(set(desc))[:self.num_limit]))+'\n'

        elif question_type == 'disease_lasttime':
            desc = [i['m.cure_lasttime'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}治疗可能持续的周期为：{1}\n'.format(subject, ','.join(list(set(desc))[:self.num_limit]))+'\n'

        elif question_type == 'disease_cureway':
            # desc = [';'.join(i['m.cure_way']) for i in answers]
            desc = [i['m.cure_way'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}可以尝试如下治疗：{1}\n'.format(subject, ','.join(list(set(desc))[:self.num_limit]))+'\n'

        elif question_type == 'disease_cureprob':
            desc = [i['m.cured_prob'] for i in answers]
            # print(desc)
            subject = answers[0]['m.name']
            final_answer = '{0}治愈的概率为（仅供参考）：{1}\n'.format(subject, ','.join(list(set(desc))[:self.num_limit]))+'\n'

        elif question_type == 'disease_easyget':
            desc = [i['m.easy_get'] for i in answers]
            # print(desc)
            subject = answers[0]['m.name']

            final_answer = '{0}的易感人群包括：{1}\n'.format(subject, ','.join(list(set(desc))[:self.num_limit]))+'\n'

        elif question_type == 'disease_dept':
            desc = [i['m.cure_department'] for i in answers]
            # desc = [i['m.cure_department'] for i in answers]
            # print(desc)
            subject = answers[0]['m.name']

            final_answer = '{0}的治疗科室包括：{1}\n'.format(subject, ''.join(list(set(desc))[:self.num_limit]))+'\n'

        elif question_type == 'disease_desc':
            desc = [i['m.desc'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}：{1}\n'.format(subject,  ','.join(list(set(desc))[:self.num_limit]))+'\n'

        elif question_type == 'disease_acompany':
            desc1 = [i['n.name'] for i in answers]
            desc2 = [i['m.name'] for i in answers]
            subject = answers[0]['m.name']
            desc = [i for i in desc1 + desc2 if i != subject]
            final_answer = '{0}的并发症状包括：{1}\n'.format(subject, ','.join(list(set(desc))[:self.num_limit]))+'\n'

        elif question_type == 'disease_not_food':
            desc = [i['n.name'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}忌食的食物包括有：{1}\n'.format(subject, ','.join(list(set(desc))[:self.num_limit]))+'\n'

        elif question_type == 'disease_do_food':
            do_desc = [i['n.name'] for i in answers if i['r.name'] == '疾病宜吃食物']
            recommand_desc = [i['n.name'] for i in answers if i['r.name'] == '疾病推荐食谱']
            subject = answers[0]['m.name']
            final_answer = '{0}宜食的食物包括有：{1} \n'.format(subject, ','.join(list(set(do_desc))[:self.num_limit]))+'\n'+'推荐食谱包括有：{1} \n'.format(subject, ','.join(list(set(recommand_desc))[:self.num_limit]))+'\n'

        elif question_type == 'food_not_disease':
            desc = [i['m.name'] for i in answers]
            subject = answers[0]['n.name']
            final_answer = '患有{0}的人最好不要吃{1}\n'.format(','.join(list(set(desc))[:self.num_limit]), subject)+'\n'

        elif question_type == 'food_do_disease':
            desc = [i['m.name'] for i in answers]
            subject = answers[0]['n.name']
            final_answer = '患有{0}的人建议多试试{1}\n'.format(','.join(list(set(desc))[:self.num_limit]), subject)+'\n'

        elif question_type == 'disease_drug':
            desc = [i['n.name'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}通常的使用的药品包括：{1}\n'.format(subject, ','.join(list(set(desc))[:self.num_limit]))+'\n'

        elif question_type == 'drug_disease':
            desc = [i['m.name'] for i in answers]
            subject = answers[0]['n.name']
            final_answer = '{0}主治的疾病有{1},可以试试\n'.format(subject, ','.join(list(set(desc))[:self.num_limit]))+'\n'

        elif question_type == 'disease_check':
            desc = [i['n.name'] for i in answers]
            subject = answers[0]['m.name']
            final_answer = '{0}通常可以通过以下方式检查出来：{1}\n'.format(subject, ','.join(list(set(desc))[:self.num_limit]))+'\n'

        elif question_type == 'check_disease':
            desc = [i['m.name'] for i in answers]
            subject = answers[0]['n.name']
            final_answer = '通常可以通过{0}检查出来的疾病有{1}\n'.format(subject, ','.join(list(set(desc))[:self.num_limit]))+'\n'

        return final_answer


if __name__ == '__main__':
    searcher = AnswerSearcher()